<?php

# langito project
# template class

class tpl {
	var $is_cached;
	var $cache;
	var $current_url;
	var $cache_php;
	var $cache_time;
	var $root_cache_dir;
	var $cache_dir;

	var $template;
	var $uri_modifier;

	function tpl () {
		$this->is_cached=false;
		$this->cache=false;
		$this->current_url=NULL;
		$this->template=NULL;
		$this->cache_php=false;
		$this->uri_modifier=NULL;
		$this->cache_time=36000; //time in seconds tpl is cached if $this->cache_time() is not set
		$this->root_cache_dir='cache/tpl/';  //default caching directory
		$this->cache_dir='';
	}
	
	function cache_php ($cache) {
		$this->cache_php=$cache;
	}
	
	function cache_dir ($dir) {
		$this->cache_dir=$dir;
	}

	function cache ($cache) {
		$this->cache=$cache;
	}
	
	function cache_time ($time) {
		$this->cache_time=$time;
	}

	function uri_modifier ($modifier) {
		$this->uri_modifier=$modifier;
	}
	
	function get_url($arr=array()) {
		$arr=array_merge($arr, array('lang', 'theme'));
		$url=array();
		foreach($_GET as $key => $value) {
			if(in_array($key, $arr) or !$_GET[$key]) continue;
			$url[]=$key.'-'.$value;
		}
		return implode("_", $url);
	}
	
	function is_cached(){
		if($this->is_cached==NULL){
			$this->current_url=$this->get_url().'_lng-'.$_SESSION['lang_short'].'_thm-'.$_SESSION['theme'].'_gid-'.$GLOBALS['gid'].$this->uri_modifier.'.tpl';
			if(file_exists($this->root_cache_dir.$this->cache_dir.$this->current_url)) {
				if((filemtime($this->root_cache_dir.$this->cache_dir.$this->current_url)+$this->cache_time)>time())
					$this->is_cached=true;
				else
					$this->is_cached=false;
			} else
				return false;
		}

		return $this->is_cached;
	}

	function create($url=NULL, $array=array()) {
		global $conf, $lang, $thm_img;
		
		if(file_exists($GLOBALS['theme_path'].$_GET['module'].'/'.$url))
			$url=$GLOBALS['theme_path'].$_GET['module'].'/'.$url;
		else
			$url=$GLOBALS['theme_path'].$url;
		
		if($this->is_cached) {
			$fp=fopen($this->root_cache_dir.$this->cache_dir.$this->current_url, 'r');
			$this->template=fread($fp, filesize($this->root_cache_dir.$this->cache_dir.$this->current_url));
			fclose($fp);
		}
		else {
			$array['theme_values']='<pre>'.print_r($array, true).'</pre>';
			$index=array_keys($array);
			
			if(!file_exists($url)) exit('Theme error: '.$url.' does\'n exists!');
			
			$fp = fopen ($url, 'r');
			$tpl = fread($fp, filesize ($url));
			fclose ($fp);

			for($i=0;$i<count($index);$i++) {
				$tpl=str_replace('{'.$index[$i].'}', $array[$index[$i]], $tpl);
			}

			$this->template.=$tpl;
		}
	}
	
	function create_string($tpl, $array=NULL) {
		global $conf, $lang, $thm_img;

		$index=array_keys($array);

		for($i=0;$i<count($index);$i++) {
			$tpl=str_replace('{'.$index[$i].'}', $array[$index[$i]], $tpl);
		}

		$this->template.=$tpl;
	}

	function save() {
		global $conf;

		if($this->is_cached==false && $conf['tpl_cache']==true && $this->cache==true) {
			$fp = fopen ($this->root_cache_dir.$this->cache_dir.$this->current_url, 'w');
			fwrite($fp, $this->template);
			fclose($fp);
		}
	}

	function parse_php() {
		global $conf, $lang, $thm_img;

		ob_start();
		if(eval(' ?' . '>' . $this->template . '<' . '?php ')===false) {
			if($GLOBALS['gid']==3){
				include_once('includes/functions/thirdparty/geshi.php');
				$this->template=htmlspecialchars($this->template);
				$this->template=str_replace('&#39;', "'", $this->template);
				$geshi = new GeSHi($this->template, 'php', 'includes/functions/thirdparty/geshi');
				$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
				$geshi=$geshi->parse_code();
				echo('<p><a href="index.php?module=admin">Admin</a></p><div id="code" class="tabelka2"><div id="code_text" class="row_1">'.$geshi.'</div></div>');
			}
			
			ob_end_flush();
		}
		else {
			$this->template=ob_get_contents();
			ob_end_clean();

			if($this->cache_php==false)
				$this->save();

		}
	}

	function parse() {
		if($this->cache_php==true)
			$this->save();


		if(($this->cache_php==true) or ($this->is_cached==false)) {
			$this->parse_php();
		}

		/*if($_GET['module']!='admin' && strpos($this->template, '[php]')!==false) { //if we have some php to cache... use [php]code;[/php] in templates
			$this->template=str_replace(array('[php]', '[/php]'), array('<?php ', ' ?>'), $this->template);
			$this->cache_php=false; //do not cache the [php][/php] from template
			$this->parse_php();
		}*/

		echo($this->template);
	}

	function get() {
		return $this->template;
	}
	
	function clear_cache($pattern, $time=false) {
		$cache=glob_($this->root_cache_dir.$this->cache_dir.$pattern);
		foreach($cache as $c) {
			@unlink($c);
			if($time) {
				if(microtime(1)-$GLOBALS['time_start']>ini_get('max_execution_time')-3){
					redirect('index.php?function=delete_cache&what=tpl');
					exit;
				}
			}
		}
	}
}


?>
